package com.pei.Dao.Impl;

import com.pei.Dao.regionDao;
import com.pei.Dao.userDao;
import com.pei.Dao.user_typeDao;
import com.pei.entity.user;
import com.pei.util.BaseDao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dell on 2018/8/6.
 */
public class userDaoImpl  implements userDao {
    @Override
    public List<user> finduser(String name, int typeid, int yenum, int jinum) {
        user_typeDao dao=new user_typeDaoImpl();
        regionDao daoo=new regionDaoImpl();
        List<user> slist = new ArrayList<user>();
        Connection conn=null;
        PreparedStatement stat=null;
        ResultSet rs = null;
        String sql = "select * from user where uname LIKE ? AND utype=? limit ?,?";
        try {
            conn = BaseDao.getConnection();
            stat = conn.prepareStatement(sql);
            stat.setString(1,"%"+name+"%");
            stat.setInt(2,typeid);
            stat.setInt(3,yenum);
            stat.setInt(4,jinum);
            rs = stat.executeQuery();
            while (rs.next()){
                user user = new user();
                user.setUid(rs.getInt("uid"));
                user.setUname(rs.getString("uname"));
                user.setUphone(rs.getString("uphone"));
                user.setUpasswrod(rs.getString("upassword"));
                user.setUprovince(rs.getInt("uprovince"));
                user.setUcity(rs.getInt("ucity"));
                user.setUcounty(rs.getInt("ucounty"));
                user.setUaddress(rs.getString("uaddress"));
                user.setUsex(rs.getString("usex"));
                user.setUtype(rs.getInt("utype"));
                user.setCreateTime(rs.getString("createTime"));
                user.setOpenid(rs.getString("openid"));
                user.setCheck(rs.getInt("check"));
                user.setUimage(rs.getString("uimage"));
                user.setUemail(rs.getString("uemail"));
                user.setUtname(dao.finduser_type(user.getUtype()));
                user.setAreaName(daoo.findregion(user.getUprovince()));
                user.setAreaName1(daoo.findregion(user.getUcity()));
                user.setAreaName2(daoo.findregion(user.getUcounty()));
                slist.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeConnection(conn,stat,rs);
        }
        return slist;
    }

    @Override
    public int findByName(String name) {
        int num=0;
        Connection conn=null;
        PreparedStatement stat=null;
        ResultSet rs=null;
        String sql="select count(1) from user where uname=?";
        try {
            conn=BaseDao.getConnection();
            stat=conn.prepareStatement(sql);
            stat.setString(1,name);
            rs=stat.executeQuery();
            if (rs.next()){
                num=rs.getInt(1);
            }
        }catch (Exception e){
            System.out.print(e.getMessage());
        }finally {
            BaseDao.closeConnection(conn,stat,rs);
        }
        return num;
    }

    @Override
    public int adduser(user u,int typeid) {
        String sql="insert into user values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] ob={
                u.getUname(),
                u.getUphone(),
                u.getUpasswrod(),
                u.getUprovince(),
                u.getUcity(),
                u.getUcounty(),
                u.getUaddress(),
                u.getUsex(),
                typeid,
                u.getCreateTime(),
                u.getOpenid(),
                u.getCheck(),
                "WaterPurifiersrcmainimages头像",
                u.getUemail()
        };
        return BaseDao.executeUpdate(ob,sql);
    }

    @Override
    public int deluser(int uid) {
        String sql="delete from user where uid=?";
        Object[] ob={
          uid
        };
        return BaseDao.executeUpdate(ob,sql);
    }

    @Override
    public user finduser(int uid) {
        user_typeDao dao=new user_typeDaoImpl();
        regionDao daoo=new regionDaoImpl();
        user user=null;
        Connection conn=null;
        PreparedStatement stat=null;
        ResultSet rs = null;
        String sql = "select * from user where uid=?";
        try {
            conn = BaseDao.getConnection();
            stat = conn.prepareStatement(sql);
            stat.setInt(1,uid);
            rs = stat.executeQuery();
            while (rs.next()){
                user = new user();
                user.setUid(rs.getInt("uid"));
                user.setUname(rs.getString("uname"));
                user.setUphone(rs.getString("uphone"));
                user.setUpasswrod(rs.getString("upassword"));
                user.setUprovince(rs.getInt("uprovince"));
                user.setUcity(rs.getInt("ucity"));
                user.setUcounty(rs.getInt("ucounty"));
                user.setUaddress(rs.getString("uaddress"));
                user.setUsex(rs.getString("usex"));
                user.setUtype(rs.getInt("utype"));
                user.setCreateTime(rs.getString("createTime"));
                user.setOpenid(rs.getString("openid"));
                user.setCheck(rs.getInt("check"));
                user.setUimage(rs.getString("uimage"));
                user.setUemail(rs.getString("uemail"));
                user.setUtname(dao.finduser_type(user.getUtype()));
                user.setAreaName(daoo.findregion(user.getUprovince()));
                user.setAreaName1(daoo.findregion(user.getUcity()));
                user.setAreaName2(daoo.findregion(user.getUcounty()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeConnection(conn,stat,rs);
        }
        return user;
    }

    @Override
    public int upuser(user u) {
        String sql="update user set uname=?,uphone=?,upassword=?,uprovince=?,ucity=?,ucounty=?,uaddress=?,usex=?,createTime=?,openid=?,`check`=?,uemail=? where uid=?";
        Object[] ob={
                u.getUname(),
                u.getUphone(),
                u.getUpasswrod(),
                u.getUprovince(),
                u.getUcity(),
                u.getUcounty(),
                u.getUaddress(),
                u.getUsex(),
                u.getCreateTime(),
                u.getOpenid(),
                u.getCheck(),
                u.getUemail(),
                u.getUid()
        };
        return BaseDao.executeUpdate(ob,sql);
    }

    @Override
    public int findcountuser(int type) {
        int num=0;
        Connection conn=null;
        PreparedStatement stat=null;
        ResultSet rs=null;
        String sql="select count(1) from user where utype=?";
        try {
            conn=BaseDao.getConnection();
            stat=conn.prepareStatement(sql);
            stat.setInt(1,type);
            rs=stat.executeQuery();
            if (rs.next()){
                num=rs.getInt(1);
            }
        }catch (Exception e){
            System.out.print(e.getMessage());
        }finally {
            BaseDao.closeConnection(conn,stat,rs);
        }
        return num;
    }
}
